﻿using System.Collections.Generic;

namespace Safebrowsing
{
    public class UrlHashEntry
    {
        internal UrlHashEntry(int chunkNumber, int hostKey, string prefix, string fullHash, SafebrowsingList list)
        {
            ChunkNumber = chunkNumber;
            HostKey = hostKey;
            Prefix = prefix;
            FullHash = fullHash;
            List = list;
        }

        public int ChunkNumber { get; private set; }
        public int HostKey { get; private set; }
        public string Prefix { get; private set; }
        public string FullHash { get; private set; }
        public SafebrowsingList List { get; private set; }

        public List<string> Hashes
        {
            get
            {
                if (FullHash == null) return null;
                int length = string.IsNullOrEmpty(FullHash) ? 0 : FullHash.Length;
                if (length < 64) return null;
                if (length % 64 != 0) return null;
                if (length == 64) return new List<string> {FullHash};
                
                var hashes = new List<string>();

                for (int i = 0; i < length / 64; i++)
                {
                    hashes.Add(FullHash.Substring(i * 64, 64));
                }

                return hashes;
            }
        }

        public override string ToString()
        {
            return string.Format("ChunkNumber={0}, HostKey={1}, Prefix={2}, FullHash={3}, List={4}", ChunkNumber, HostKey, Prefix,
                                 FullHash, List.Name);
        }
    }
}
